<template>
  <div>
    <el-card>
      <el-form :model="pwd_Form" :rules="rules" ref="pwd_Form" label-width="120px">
        <el-form-item label="原密码" prop="u_pwd">
          <el-input v-model="pwd_Form.u_pwd" placeholder="请输入原密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="nu_pwd">
          <el-input v-model="pwd_Form.nu_pwd" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="rnu_pwd">
          <el-input v-model="pwd_Form.rnu_pwd" placeholder="请再次输入新密码"></el-input>
        </el-form-item>
       <el-row>
         <el-button type="primary" @click="updatePwd">提交</el-button>
         <el-button @click="handleCancel">取消</el-button>
       </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "ChangePwd",
  data() {
    return {
      formData:new FormData(),//表单类型的数据对象，可以包含文件
      pwd_Form: {
        u_pwd: "",
        nu_pwd: "",
        rnu_pwd: "",
      },
     rules: {
        u_pwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 3, message: '原密码长度至少为3位', trigger: 'blur' },
        ],
       nu_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 3, message: '新密码长度至少为3位', trigger: 'blur' },
        ],
       rnu_pwd: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' },
          { validator: this.validateConfirmPassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    // 验证确认密码是否和新密码一致的方法
    validateConfirmPassword(rule, value, callback) {
      if (value === this.pwd_Form.nu_pwd) {
        callback();
      } else {
        callback(new Error('两次输入的新密码不一致'));
      }
    },
    handleCancel() {
      // 重置表单数据
      this.$refs.pwd_Form.resetFields();
    },
    // 提交表单
    async updatePwd() {
      this.$refs.pwd_Form.validate(async (valid) => {
        if (valid) {
          try {
            // 验证旧密码
            const { data: oldpwdRes } = await this.$axios.get("/user/checkOldPwd", {
              params: {
                u_id: sessionStorage.getItem("u_id"),
                u_pwd: this.pwd_Form.u_pwd,
              },
            });

            if (oldpwdRes.success) {
              // 旧密码正确，更新新密码
              const { data: res } = await this.$axios.put("/user/addNewPwd", {
                u_id: sessionStorage.getItem("u_id"),
                u_pwd: this.pwd_Form.nu_pwd,
              });

              if (res.success) {
                this.$message.success("修改成功");
                this.dialogVisible = false;
                this.resetForm(); // 重置表单数据
              } else {
                this.$message.error("密码更新失败，请重试");
              }
            } else {
              this.$message.error("旧密码错误，请重试");
            }
          } catch (error) {
            this.$message.error("修改失败，请重试");
            console.error(error);
          }
        } else {
          this.$message.error("请检查表单信息");
          return false;
        }
      });
    },
    // 重置表单数据
    resetForm() {
      this.$refs.pwd_Form.resetFields();
    },

  }
};
</script>

<style scoped>
.el-card {
  width: 400px;
  margin: 100px auto; /* 让卡片在页面中水平居中 */
}
.dialog-footer {
  text-align: right; /* 让按钮靠右对齐 */
  padding: 10px 0;
}
</style>